package interaction

import (
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/interaction"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
)

type UserRelationApi struct{}

// Follow 关注
func (api *UserRelationApi) Follow(ctx *gin.Context) {
	selfId := utils.GetUserID(ctx)
	recipientUUID := ctx.DefaultQuery("userUUID", "")
	if recipientUUID == "" {
		response.FailWithMessage("缺少userUUID", ctx)
		return
	}

	// 通过UUID获取用户ID
	recipientId, err := userRelationService.GetUserIdByUUID(recipientUUID)
	if err != nil {
		response.FailWithMessage("用户不存在", ctx)
		return
	}

	err = userRelationService.SetUserBetweenUserRelation(selfId, recipientId, interaction.FollowCode)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	response.OkWithMessage("关注成功...", ctx)
}

// UnFollow 取消关注
func (api *UserRelationApi) UnFollow(ctx *gin.Context) {
	selfId := utils.GetUserID(ctx)
	recipientUUID := ctx.DefaultQuery("userUUID", "")
	if recipientUUID == "" {
		response.FailWithMessage("缺少userUUID", ctx)
		return
	}

	// 通过UUID获取用户ID
	recipientId, err := userRelationService.GetUserIdByUUID(recipientUUID)
	if err != nil {
		response.FailWithMessage("用户不存在", ctx)
		return
	}

	err = userRelationService.SetUserBetweenUserRelation(selfId, recipientId, interaction.UnFollowCode)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	response.OkWithMessage("取消关注成功...", ctx)
}

// BlockFollow 屏蔽关注
func (api *UserRelationApi) BlockFollow(ctx *gin.Context) {
	selfId := utils.GetUserID(ctx)
	recipientUUID := ctx.DefaultQuery("userUUID", "")
	if recipientUUID == "" {
		response.FailWithMessage("缺少userUUID", ctx)
		return
	}

	// 通过UUID获取用户ID
	recipientId, err := userRelationService.GetUserIdByUUID(recipientUUID)
	if err != nil {
		response.FailWithMessage("用户不存在", ctx)
		return
	}

	err = userRelationService.SetUserBetweenUserRelation(recipientId, selfId, interaction.BlockFollow)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	response.OkWithMessage("屏蔽成功...", ctx)

}

// GetSelfFanList 获取自身粉丝列表
func (api *UserRelationApi) GetSelfFanList(ctx *gin.Context) {
	selfId := utils.GetUserID(ctx)
	var params request.PageInfo
	err := ctx.ShouldBindJSON(&params)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}

	result, total, err := userRelationService.GetUserRelationList(selfId, params, interaction.Fan)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     result,
		Total:    total,
		Page:     params.Page,
		PageSize: params.PageSize,
	}, "获取成功", ctx)
}

// GetSelfIdolList 获取自身偶像列表
func (api *UserRelationApi) GetSelfIdolList(ctx *gin.Context) {
	selfId := utils.GetUserID(ctx)
	var params request.PageInfo
	err := ctx.ShouldBindJSON(&params)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	result, total, err := userRelationService.GetUserRelationList(selfId, params, interaction.Idol)
	if err != nil {
		response.FailWithMessage(err.Error(), ctx)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     result,
		Total:    total,
		Page:     params.Page,
		PageSize: params.PageSize,
	}, "获取成功", ctx)

}
